package list;

import java.util.HashSet;
import java.util.Set;

public class LeetCode160 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA ==null || headB==null) return null;
        Set<ListNode> set = new HashSet<>();
        ListNode p1 = headA;
        while (p1 != null) {
            set.add(p1);
            p1 = p1.next;
        }

        ListNode p2 = headB;
        while (p2 != null) {
            if (!set.add(p2)) {
                System.out.println("-----");
                break;
            }
            p2 = p2.next;
        }

        return p2;
    }
}
